var pathName=window.document.location.pathname;
var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
function init(classifyId,path,filePath){
	var pathName=window.document.location.pathname;
	var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
$.ajax({
	type : 'POST',
	url : projectName+"/classify/children",
	data :"classifyId="+classifyId,
	async : false,
	success : function(data,status){ 
        if(status == "success"){  
            var objs = eval(jQuery.parseJSON(data));  
            var name = "";
            var id = "";
            var picture = "";
            var html = "";
            var i = 1;
            html += "<table>";
            for(var key in objs){
            	id = objs[key].classify_id;
            	name = objs[key].classify_name;
            	picture = objs[key].classify_picture;
            	if(i%3==1){
            		html += "<tr><td>";
            		html += "<div onclick=\"getArticleList('"+id+"',1)\" style='padding-top:5px'><img width=200 height=200 src='"+path+"/"+filePath+"/"+picture+"'></img><p align='center'>"+name+"</p></div>"
            		html += "</td>";
            	}
            	if(i%3==2){
            		html += "<td>"
            		html += "<div onclick=\"getArticleList('"+id+"',1)\" style='float:left;padding-left:60px;padding-top:5px'><img width=200 height=200 src='"+path+"/"+filePath+"/"+picture+"'></img><p align='center'>"+name+"</p></div>"
            		html += "</td>";
            	}
            	if(i%3==0){
            		html += "<td>";
            		html += "<div onclick=\"getArticleList('"+id+"',1)\" style='float:right;padding-left:60px;padding-top:5px'><img width=200 height=200 src='"+path+"/"+filePath+"/"+picture+"'></img><p align='center'>"+name+"</p></div>"
            		html += "</td>";
            		html += "</tr>"
            	}
            	i++;
            	//html += "<div id=\""+id+"\"><li><a href='#' onclick=\"getClassifysById('"+id+"','"+name+"')\"><span>"+name+"</span></a></li></div>";
            }
            if(i%3!=0){
            	html += "</tr>"
            }
            $("#content_1").html(html);
        }  
	}
});
}

function getArticleList(classifyId,page){
	var pathName=window.document.location.pathname;
	var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
$.ajax({
	type : 'POST',
	url : projectName+"/article/lists",
	data :"classifyId="+classifyId+"&page="+page,
	async : false,
	success : function(data,status){ 
        if(status == "success"){  
            var objs = eval(jQuery.parseJSON(data));  
            var html = "";
            var i = 1;
            html += "<table class='table1'>";
            html += "<tr><th>标题</th><th>作者</th><th>创建时间</th></tr>";
            //加载数据
            for(var key in objs){
            	if(key != "page"){
            	var id = objs[key].article_id;
            	var title = objs[key].article_title;
            	var author = objs[key].article_author;
            	var time = objs[key].article_createTime;
            	html +="<tr style='cursor: pointer;' onclick=\"getArticle('"+id+"')\"><td>"+title+"</td><td>"+author+"</td><td>"+time+"</td></tr>";
            	}
            }
            html +="</table>";
            html += "<div style='padding-bottom:3%'><a href='#' onclick=\"getArticleList('"+classifyId+"',"+objs["page"].lastPage+")\">上一页</a>" +
            		"&nbsp;第"+objs['page'].page+"页" +
            		"&nbsp;<a href='#' onclick=\"getArticleList('"+classifyId+"',"+objs["page"].nextPage+")\">下一页</a>" +
            		"&nbsp;总页数："+objs["page"].totalPage+
            		"</div>";
            $("#content_1").html(html);
        }  
	}
});
	
}
function getArticle(articleId){
	var pathName=window.document.location.pathname;
	var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
$.ajax({
	type : 'POST',
	url : projectName+"/article/show",
	data :"articleId="+articleId,
	async : false,
	success : function(data,status){ 
        if(status == "success"){  
        	loagArticle(data);
        }  
	}
});
}
function loagArticle(data){
	var objs = eval(jQuery.parseJSON(data));  
    var html = "";
    var i = 1;
    html += "<div>";
    //加载数据
    	var id = objs["article"].article_id;
    	var title = objs["article"].article_title;
    	var author = objs["article"].article_author;
    	var time = objs["article"].article_createTime;
    	var content = decodeURIComponent(objs["content"]);
    html += "<input type='hidden' id='articleId' value=\""+id+"\"/>" ;
    html += "<p align='center' style='font-size: xx-large;'>"+title+"</p>";
    html += "<p align='center' style='font-size: x-large;'>"+author+"&nbsp;&nbsp;&nbsp;"+time+"</p>";
    html += "<div style='border:3px solid #F1DFDF;padding-top:5px;padding-left:5px;'>"+content+"</div>";
    html += "<div><br><hr>";
    var i = 1;
    html +="<p>留言板</p>"
    for(var key in objs){
    	if(key == "article" || key == "content"){
    		continue;
    	}
    	html += "<p>"+i+"楼：&nbsp;"+objs[key].msg_content+"................."+objs[key].msg_author+"&nbsp;&nbsp;"+objs[key].msg_createTime+"</p>";
    	i++;
    }
    html += "<br>留言：<input id='message_content' size=30 maxLenth=30/> &nbsp;<input type='button' onclick='addMessage()' value='提交'/>"
    html += "</div>";
    html +="</div>";
    $("#content_1").html(html);
}
//登录
function login(){
	var userName = document.getElementById("username").value;
	var passwd = document.getElementById("passwd").value;
	$.ajax({
		type : 'POST',
		url : projectName+"/user/userlogin",
		data :"username="+userName+"&passwd="+passwd,
		async : false,
		success : function(data,status){ 
	       window.location.reload();
		}
	});
}
//注销
function logout(){
	$.ajax({
		type : 'POST',
		url : projectName+"/user/logout",
		async : false,
		success : function(data,status){ 
			 window.location.reload();
		}
	});
}
//验证用户名是否存在
function checkUser(username){
	var userName = username.value;
	$.ajax({
		type : 'POST',
		url : projectName+"/user/checkuser",
		data : "userName="+userName,
		async : false,
		success : function(data,status){ 
			if(statue="success"){
				var objs = eval(jQuery.parseJSON(data));  
				var successMsg  = objs["true"];
				var errorMsg  = objs["false"];
				if(successMsg != undefined){
					$("#userName_msg").css('color','green');
					$("#name_flag").val("true");
					$("#userName_msg").html(successMsg);
				}
				if(errorMsg != undefined){
					$("#userName_msg").css('color','red');
					$("#name_flag").val("false");
					$("#userName_msg").html(errorMsg);
				}
			}
			
		}
	});
}
//验证密码是否一致
function checkPass(passwd){
	var password = document.getElementById("pass_word").value;
	passwd = passwd.value;
	password = password.replace(/(^\s*)|(\s*$)/g, ""); 
	passwd = passwd.replace(/(^\s*)|(\s*$)/g, ""); 
	var msg = "";
	if("" == passwd || "" == password){
		msg = "密码不能为空！"
	}else if(passwd != password){
		msg = "两次密码不一样！"
	}
	$("#pass_msg").css('color','red');
	$("#pass_msg").html(msg);
	
}
function addUser(){
	var name = $("#user_name").val();
	name = name.replace(/(^\s*)|(\s*$)/g, "");
	var pass_word  = $("#pass_word").val();
	pass_word = pass_word.replace(/(^\s*)|(\s*$)/g, "");
	var pass_wd  = $("#pass_wd").val();
	pass_wd = pass_wd.replace(/(^\s*)|(\s*$)/g, "");
	var flag = $("#name_flag").val();
	if("false"==flag){
		alert("用户名不合法！");
		return;
	}
	if(""==name){
		alert("用户名不能为空！");
		return;
	}
	if(""==pass_word || ""==pass_wd){
		alert("密码不能不能为空！");
		return;
	}
	if(pass_word != pass_wd){
		alert("两次密码输入不一致!")
		return;
	}
	$.ajax({
		type : 'POST',
		url : projectName+"/user/adduser",
		data : "userName="+name+"&passWord="+pass_word,
		async : false,
		success : function(data,status){ 
			if(statue="success"){
				alert("注册成功！");
				$("#name_flag").value="false";
			}
			
		}
	});
}
//获取用户详细信息
function getUserMsg(userId){
	$.ajax({
		type : 'POST',
		url : projectName+"/user/getuser",
		data : "userId="+userId,
		async : false,
		success : function(data,status){ 
			if(statue="success"){
				loadUserMsg(data);
			}
			
		}
	});
}
function updateUser(){
	var formData = $('#myFORM').serialize();
	$.ajax({
		type : 'POST',
		url : projectName+"/user/updateuser",
		data : formData,
		async : false,
		success : function(data,status){ 
			if(statue="success"){
				alert("成功更新!");
				loadUserMsg(data);
			}
			
		}
	});
}
function loadUserMsg(data){
	var objs = eval(jQuery.parseJSON(data));
	var html = "<div><fieldset>" +
			"<form class='JoinMemForm' id='myFORM' name='myFORM' method='post' novalidate='novalidate'>";
	html += "<input type='hidden' name='userId' value=\""+objs["user"].user_id+"\"/>";
	html += "<p><lable>用户名："+objs["user"].user_name+"</label></p><br>";
	html += "<p>年龄：<input maxlenth=3 type='text' name='userAge' value=\""+objs["user"].user_age+"\" onKeyPress=\"if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;\" /></p><br>";
	if(objs["user"].user_sex=="男"){
	html += "<p>性别：<input type='radio' name='userSex' value='男' checked>男" +
			"&nbsp;&nbsp;<input type='radio' name='userSex' value='女'>女" +
			"</p><br>";
	}else{
		html += "<p>性别：<input type='radio' name='userSex' value='男' />男" +
				"&nbsp;&nbsp;<input type='radio' name='userSex' value='女' checked />女" +
				"</p><br>";
	}
	html += "<p>邮箱：<input type='text' name='userEmail' value=\""+objs["user"].user_mail+"\"/></p><br>";
	html += "<p>电话：<input type='text' name='userTel' value=\""+objs["user"].user_tel+"\"/></p><br>";
	html += "<p><label>注册时间："+objs["user"].user_regtime+"</label></p><br>";
	html += "<input type='button' value='更新' onclick='updateUser()'/>&nbsp;<input type='reset' value='重置'/>"
	html += "</fieldset></form></div>";
	html += "<hr>";
	html += "<select id='classifyId'>";
	for(var key in objs) {
		if(key != "user"){
		html += "<option value=\""+objs[key].classify_id+"\">"+objs[key].classify_name+"</option>";
		}
	}
	html += "</select>";
	html += "&nbsp;&nbsp;<a href='#' onclick=\"toAddArticle()\">添加游记</a>";
	$("#content_1").html(html);
	
}
//跳转到添加文章界面
function toAddArticle(articleId){
	if(articleId == undefined){
		articleId = null;
	}
	var classifyId = document.getElementById("classifyId").value;
	if(classifyId==""){
		alert("请选择分类!");
		return;
	}
	var pathName=window.document.location.pathname;
	var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
	window.open(projectName+"/article/toadd?classifyId="+classifyId+"&articleId="+articleId);
}
function addMessage(){
	var articleId = document.getElementById("articleId").value;
	var message_content = document.getElementById("message_content").value;
	$.ajax({
		type : 'POST',
		url : projectName+"/article/addMessage",
		data :"articleId="+articleId+"&msg_content="+message_content,
		async : false,
		success : function(data,status){ 
	        if(status == "success"){  
	        	loagArticle(data);
	        }  
		}
	});
}
//首页初始化
function initIndex(){
	$.ajax({
		type : 'POST',
		url : projectName+"/article/initIndex",
		async : false,
		success : function(data,status){ 
	        if(status == "success"){  
	        	var objs = eval(jQuery.parseJSON(data));
	        	var html = "<br>";
	        	for(var key in objs){
	        		html += "<p onclick=\"toArticle('"+objs[key].article_id+"','"+objs[key].article_classifyId+"')\">"+objs[key].article_title+"......."+objs[key].article_author+"......"+objs[key].article_createTime+"</p>";
	        	}
	        	html += "<br>";
	        	$("#articlelist").html(html);
	        }  
		}
	});
}
function toArticle(articleId,classifyId){
	var p_classifyId = "";
	$.ajax({
		type : 'POST',
		url : projectName+"/classify/getparent",
		data : "classifyId="+classifyId,
		async : false,
		success : function(data,status){ 
	        if(status == "success"){  
	        	var objs = eval(jQuery.parseJSON(data));
	        	p_classifyId = objs["p_classifyId"];
	        }  
		}
	});
	
	var targetJsp = "";
	if("RjdM4lc8HKLxsYKuURCHeqvN2XLJ66Ks" == p_classifyId){
		targetJsp = "guanli.jsp";
	}
	if("tBxzs5Xpi9qXhkcALqzJYJTjS6rOWcZS" == p_classifyId){
		targetJsp = "hotel.jsp";
	}
	if("AHvltSuH1IrHOtoX5SK2HlFNZ0syJMgB" == p_classifyId){
		targetJsp = "news_list.jsp";
	}
	if("otYQfEvHQvhtUhfTM5yb3AFzcUWbGEkz" == p_classifyId){
		targetJsp = "recommend.jsp";
	}
	if("ut5OroI3brPY8CofAirtbSVgUF9ITga4" == p_classifyId){
		targetJsp = "zhuce.jsp";
	}
	window.location.href=targetJsp+"?articleId="+articleId;
}
